package com.lxf.cloudproduct.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.lxf.cloudproduct.bean.TaskBean;
import com.lxf.cloudproduct.mapper.TaskMapper;
import com.lxf.cloudproduct.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 罗兴发
 * @data 2025/6/9 15:02
 * @description：任务-业务实现层
 */
@Service
@Slf4j
public class TaskServiceImpl implements TaskService {
    @Resource
    TaskMapper taskMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)//开启事务
    public void addTask(JSONObject json) {

        JSONObject task = json.getJSONObject("task");

        Integer userId = task.getInteger("user_id");
        String tContent = task.getString("content");

        //对象实例化
        TaskBean t = new TaskBean();
        t.setUserId(userId);
        t.setTContent(tContent);

        //list装载
        List<TaskBean> list = new ArrayList<>();
        list.add(t);

        //添加操作
        int flag = taskMapper.addTask(list);
        if (flag<0)log.info("Insert Completed");
        else log.info("Insert Fails");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)//开启事务
    public void updateTask(JSONObject json) {

        JSONObject task = json.getJSONObject("task");

        Integer tId = task.getInteger("id");
        Integer tStatus = task.getInteger("status");
        String tContent = task.getString("content");

        //对象实例化
        TaskBean t = new TaskBean();
        t.setTId(tId);
        t.setTStatus(tStatus);
        t.setTContent(tContent);

        //更新操作
        int flag = taskMapper.updateTask(t);
        if (flag<0)log.info("Update Completed");
        else log.info("Update Fails");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)//开启事务
    public void deleteTask(JSONObject json) {
        Integer id = json.getInteger("id");
        int flag = taskMapper.deleteTask(id);
        if (flag<0)log.info("Delete Completed");
        else log.info("Delete Fails");
    }

    @Override
    public List<TaskBean> getAllTask(JSONObject json) {
        return taskMapper.getAllTask();
    }
}
